(function () {
    /**
     * 理牌界面扩展
     * @class SortCards
     * @constructor
     */
    function SortCards() {
        SortCards.super(this);

        this.CardUI = [];
        this.Cards  = [];
        this.Tabs   = [];
        this.CurrentCards = [];
        this.ExchangeCurrentName = null;
        this.ExchangeTargetName  = null;
        this.ExchangeCurrentCard = null;
        this.ExchangeTargetCard  = null;
        this.closeHandler = null;
        this.isPopup = false;
        this.custom = true;

        //this.createCards();
        this.initEvent();
    }
    Laya.class(SortCards, "ui.Alert.SortCards", ui.Room.SortCardUI);

    var _proto = SortCards.prototype;
    /**
     * @privite
     * 创建扑克牌UI
     */
    _proto.createCards = function () {
        var CardUI = this.CardUI = [];
        var CardPanel = this.CardPanel;

        utils.log("SortCards.createCards");
        utils.log(CardUI, CardPanel);

        CardPanel.removeChildren();
        for(var i = 0; i < 13; i++){
            (function(i, CardUI, CardPanel){
                var card = new ui.Com.Card(null, true);
                card.enableclick = true;
                card.index = i;
                card.name = "Card." + i;

                if(i < 3){
                    card.x = (i + 1) * 138 + 38;
                }else{
                    var xindex = (i - 3) % 5;
                    var yindex = (i + 2) / 5 | 0;
                    card.x = xindex * 138 + 38;
                    card.y = yindex * 180;
                }

                CardPanel.addChild(card);
                CardUI.push(card);
            })(i, CardUI, CardPanel);
        }
    }
    /**
     * @privite
     * 绑定List组件的选择回调，出牌及扑克牌的点击事件
     */
    _proto.initEvent = function () {
        utils.log("SortCards.initEvent");
        this.CardType.selectHandler = Laya.Handler.create(this, this.onCardTypeTabSelected, null, false);
        //出牌按钮
        this.btnPlay.on(Laya.Event.CLICK, this, function () {
            var currentCards = this.CurrentCards;
            utils.log("点击出牌按钮，检查是否 相公");
            utils.log(currentCards);
            viewer.publish("io.emit", {
                cmd : "game::check",
                params : {
                    token : token,
                    poker : currentCards
                }
            });
        });

        //理牌界面，交换扑克牌
        this.CardPanel.on(Laya.Event.CLICK, this, function(e){
            var name = e.target.name;
            var card = this.CardPanel.getChildByName(name);
            if(!card){return;}
            var selected = card.selected;

            this.CardType.selectedIndex = -1;

            if(!this.ExchangeCurrentName){
                if(!selected){return;}
                this.ExchangeCurrentName = name;
            }else{
                if(this.ExchangeCurrentName === name){
                    this.ExchangeCurrentName = null;
                }else{
                    this.ExchangeTargetName = name;
                }
            }

            this.exchangeCards();
        });
    }
    /**
     * @privite
     * 交换扑克牌位置
     */
    _proto.exchangeCards = function () {
        if(!this.ExchangeCurrentName || !this.ExchangeTargetName){return;}
        utils.log("===========exchangeCards===========");

        this.CardType.selectedIndex = -1;
        
        var currentCard = this.CardPanel.getChildByName(this.ExchangeCurrentName);
        var targetCard  = this.CardPanel.getChildByName(this.ExchangeTargetName);
        var temp = {};
        var tempCardPoint = this.CurrentCards[currentCard.index];

        currentCard.selected = false;
        targetCard.selected  = false;


        this.CurrentCards[currentCard.index] = this.CurrentCards[targetCard.index];
        this.CurrentCards[targetCard.index]  = tempCardPoint;

        temp.index = currentCard.index;
        temp.name = currentCard.name;
        temp.x = currentCard.x;
        temp.y = currentCard.y;

        //currentCard.x = targetCard.x;
        //currentCard.y = targetCard.y;
        currentCard.index = targetCard.index;
        currentCard.name = targetCard.name;
        Laya.Tween.to(currentCard, {x : targetCard.x, y : targetCard.y}, 100);

        //targetCard.x = temp.x;
        //targetCard.y = temp.y;
        targetCard.index = temp.index;
        targetCard.name = temp.name;
        Laya.Tween.to(targetCard, {x : temp.x, y : temp.y}, 100);


        currentCard = null;
        targetCard = null;
        tempCardPoint = null;
        temp = null;

        this.ExchangeCurrentName = null;
        this.ExchangeTargetName  = null;

        utils.log(this.CurrentCards);
    }
    /**
     * @privite
     * 更新理牌界面的扑克牌点数
     * 
     * @param {Array} cards 当前13张扑克牌的点数
     */
    _proto.updateCards = function (cards){
        var CardUI = this.CardUI;
        this.CurrentCards = cards.concat();
        
        utils.log("切换推荐的扑克牌组合");
        utils.log(this.CurrentCards);

        for(var i = 0; i < 13; i++){
            var card = this.CardPanel.getChildByName("Card." + i);
            utils.log(card.point, card.name, cards[i]);
            card.point = cards[i];
        }
    }
    /**
     * @privite
     * 理牌界面tab点击事件
     * 
     * @param {Number} index 当前选中项索引值
     */
    _proto.onCardTypeTabSelected = function(index){
        utils.log("TAB:" + index);
        var btnNums = this.CardType.length;
        var Tabs = this.Tabs;

        for(var i = 0; i < btnNums; i++){
            var itemData = Tabs[i];
            if(i === index){
                this.CardType.setItem(index, {
                    bg     : 1,
                    first  : {text : itemData.first, color : "#ffffff"},
                    second : {text : itemData.second, color : "#ffffff"},
                    third  : {text : itemData.third, color : "#ffffff"}
                });
            }else{
                this.CardType.setItem(i, {
                    bg     : 0,
                    first  : {text : itemData.first, color : "#f3ba7e"},
                    second : {text : itemData.second, color : "#f3ba7e"},
                    third  : {text : itemData.third, color : "#f3ba7e"}
                });
            }
        }

        if(index === -1){return;}
        this.updateCards(this.Cards[index]);
    }

    /**
     * @public
     * 显示理牌界面
     * 
     * @param {Object} data 理牌相关数据
     */
    _proto.display = function (data) {
        this.isPopup = true;
        this.width = Laya.stage.width;

        var tabs = this.Tabs = [];
        var cards = this.Cards = [];

        utils.log("SortCards.show---->>");
        utils.log(tabs);
        utils.log(cards);

        this.createCards();

        for(var i in data){
            var _data = data[i];
            var _type = _data.type;
            cards.push(_data.card);
            tabs.push({
                first  : _type[0],
                second : _type[1],
                third  : _type[2],
            });
        }
        
        for(var i in cards){
            utils.log(i, cards[i]);
        }

        this.CardType.array = []; //需先重置List数据，否则第一项的数据还是赋值以前的
        this.CardType.array = tabs.concat(); //使用concat深拷贝tabs，否则在Laya的List组件里会改变tabs的数组结构
        this.CardType.selectedIndex = 0;

        Arena.Stage.addChild(this);
    }
    /**
     * @public
     * 关闭弹窗
     */
    _proto.close = function (name) {
        this.isPopup = false;
        this.removeSelf();
        this.closeHandler && this.closeHandler.runWith(name);
    }
})();